//
//  LGDoctorAdviceDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/16.
//  医嘱详情

#import "LGDoctorAdviceDetailController.h"
//药物调整记录
#import "LGMedicneRecordController.h"
#import "LGFollowDetailMedicineCell.h"
#import "LGFollowDetailNextPlanCell.h"
#import "LGFollowDetailMonitorViewCell.h"
#import "LGFollowDetailSuggestCell.h"
#import "LGFollowNoneDataHeader.h"
#import "LGFollowEmptySectionView.h"
@interface LGDoctorAdviceDetailController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UIButton *medicinRecordBtn;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**  医嘱-用药方案 */
@property (nonatomic , strong)NSArray *medicPlanArray;
/**  医嘱-下次复诊计划 */
@property (nonatomic , strong)NSDictionary *visitDic;
/**  医嘱-健康监测 */
@property (nonatomic , strong)NSDictionary *monitorDic;
/**  医嘱-随访建议 */
@property (nonatomic , copy)NSString *otherSuggest;
/**   */
@property (nonatomic , assign)BOOL haveAdvice;
@end

@implementation LGDoctorAdviceDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"医嘱详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/medical_advice.medical_advice/detail" parameters:@{@"record_id":self.recordid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.medicPlanArray = returnData[@"medicines"];
            self.visitDic = returnData[@"visit"];
            self.monitorDic = returnData[@"monitor"];
            self.otherSuggest = LGNSString(returnData[@"other"][@"suggest"]);
            if(self.medicPlanArray.count>0 || self.visitDic.allKeys.count>0 || self.monitorDic.allKeys.count>0 || self.otherSuggest.length>0){
                self.haveAdvice = YES;
            }else{
                self.haveAdvice = NO;
            }
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//用药方案-调整记录
-(void)checkMedicinRecordAction{
    LGMedicneRecordController *controller = [[LGMedicneRecordController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0){
        //医嘱--用药方案
        return self.medicPlanArray.count;
        
    }else if (section == 1  && self.visitDic.allKeys.count>0){
        //医嘱--下次复诊计划
        return 1;
        
    }else if (section == 2 && self.monitorDic.allKeys.count>0){
        //医嘱--健康监测
        return self.monitorDic.allKeys.count;
        
    }else if (section == 3 && self.otherSuggest.length>0){
        //医嘱--随访建议
        return 1;
        
    }else{
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    CGFloat height = 2;
    if((section == 0 && self.medicPlanArray.count>0) || (section == 1 && self.visitDic.allKeys.count>0) || (section == 2 && self.monitorDic.allKeys.count>0) || (section == 3 && self.otherSuggest.length>0) ){
        //用药方案、下次复诊计划、健康监测、随访建议
        height = viewPix(55);
    }
    return height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if((section == 0 && self.medicPlanArray.count>0) || (section == 1 && self.visitDic.allKeys.count>0) || (section == 2 && self.monitorDic.allKeys.count>0) || (section == 3 && self.otherSuggest.length>0)){
        NSString *key = [NSString stringWithFormat:@"%ld",section];
        NSDictionary *titleDic = @{@"0":@"用药方案",@"1":@"下次复诊计划",@"2":@"健康监测",@"3":@"随访建议",};
        LGFollowNoneDataHeader *noneHeader = [[LGFollowNoneDataHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(55))];
        noneHeader.title = titleDic[key];
        noneHeader.subTitle = @" ";
        if(section == 0){
            [noneHeader addSubview:self.medicinRecordBtn];
        }
        return noneHeader;
    }
    LGFollowEmptySectionView *emptyView = [[LGFollowEmptySectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 2)];
    return emptyView;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        //用药方案
        LGFollowDetailMedicineCell *medicineCell = [tableView dequeueReusableCellWithIdentifier:@"MedicineCell" forIndexPath:indexPath];
        medicineCell.selectionStyle = UITableViewCellSelectionStyleNone;
        medicineCell.dataDic = self.medicPlanArray[indexPath.row];
        return medicineCell;
        
    }else if (indexPath.section == 1){
        //下次复诊计划
        LGFollowDetailNextPlanCell *planCell = [tableView dequeueReusableCellWithIdentifier:@"PlanCell" forIndexPath:indexPath];
        planCell.selectionStyle = UITableViewCellSelectionStyleNone;
        planCell.dataDic = self.visitDic;
        return planCell;
        
    }else if (indexPath.section == 2){
        //健康监测
        LGFollowDetailMonitorViewCell *monitorCell = [tableView dequeueReusableCellWithIdentifier:@"MonitorCell" forIndexPath:indexPath];
        monitorCell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *keyArray = self.monitorDic.allKeys;
        monitorCell.dataDic = self.monitorDic[keyArray[indexPath.row]];
        return monitorCell;
    }else if (indexPath.section == 3){
        //随访建议
        LGFollowDetailSuggestCell *suggestCell = [tableView dequeueReusableCellWithIdentifier:@"SuggestCell" forIndexPath:indexPath];
        suggestCell.selectionStyle = UITableViewCellSelectionStyleNone;
        suggestCell.content = self.otherSuggest;
        return suggestCell;
        
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if ((section == 1 && self.visitDic.allKeys.count == 0) || (section == 2 && self.monitorDic.allKeys.count == 0) || (section == 3 && self.otherSuggest.length == 0)){
        return 0.01;
        
    }else{
        return viewPix(15);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if ((section == 1 && self.visitDic.allKeys.count==0) || (section == 2 && self.monitorDic.allKeys.count==0) || (section == 3 && self.otherSuggest.length==0)){
        return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }else{
        LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(10))];
        footerView.bottomMargin = viewPix(10);
        return footerView;
    }
}



-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGFollowDetailMedicineCell class] forCellReuseIdentifier:@"MedicineCell"];
        [_tableView registerClass:[LGFollowDetailNextPlanCell class] forCellReuseIdentifier:@"PlanCell"];
        [_tableView registerClass:[LGFollowDetailMonitorViewCell class] forCellReuseIdentifier:@"MonitorCell"];
        [_tableView registerClass:[LGFollowDetailSuggestCell class] forCellReuseIdentifier:@"SuggestCell"];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
//        _tableView.tableHeaderView = self.headView;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
    }
    return _tableView;
}

-(UIButton *)medicinRecordBtn{
    if(!_medicinRecordBtn){
        _medicinRecordBtn = [UIButton buttonWithTitle:@"查看调整记录" titleFont:LGFont(14) textColor:@"#333333" imageName:@"grayTip12"];
        _medicinRecordBtn.frame = CGRectMake(Screen_W-viewPix(228), viewPix(8), viewPix(200), viewPix(48));
        _medicinRecordBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_medicinRecordBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
        [_medicinRecordBtn addTarget:self action:@selector(checkMedicinRecordAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _medicinRecordBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
